package com.zxt.user.security.handle;

import com.zxt.bean.LoginUserInfoApiVO;
import com.zxt.user.security.service.TokenService;
import com.zxt.user.api.utils.UserUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * 自定义退出处理类 返回成功
 */
@Configuration
@AllArgsConstructor
//@ComponentScan
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    private  TokenService tokenService;

    /**
     * 退出处理
     *
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        LoginUserInfoApiVO loginUser = UserUtil.getLoginUser(request);
        if (Objects.nonNull(loginUser)) {
            // 删除用户缓存记录
            tokenService.deleteLoginUser(loginUser.getToken());
        }
    }
}
